<template>
  <div class="user">
    <div class="header">
      <h2>用户信息</h2>
      <div>
        <el-button type="primary" @click="add">新增</el-button>
      </div>
      <hr />
    </div>
    <div class="content">
      <el-table v-loading="loading" :data="tableData" border>
        <el-table-column
          label="用户编号"
          prop="userId"
          align="center"
        ></el-table-column>
        <el-table-column
          label="用户名称"
          prop="userName"
          align="center"
        ></el-table-column>
        <el-table-column label="角色状态" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.status == 0 ? "正常" : "禁用" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="edit(scope.row)">修改</el-button>
            <el-button @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="footer">
      <pagination
        :current-page="pageDate.pageNum"
        :page-size="pageDate.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div v-if="maskFlag" class="mask">
      <div class="table">
        <div class="flex">
          <div class="title">{{ title }}</div>
          <i class="el-icon-close" @click="close"></i>
        </div>
        <div class="custom">
          <el-form
            label-position="left"
            label-width="100px"
            ref="ruleForm"
            :rules="rules"
            :model="form"
          >
            <el-form-item label="用户类型">
              <el-radio-group v-model="userType">
                <el-radio :label="0">承包区</el-radio>
                <el-radio :label="1">客户</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              v-if="userType == 0"
              label="所属承包区"
              prop="nickName"
            >
              <el-select v-model="form.nickName" placeholder="请选择承包区">
                <el-option
                  v-for="item in networkList"
                  :key="item.id"
                  :label="item.networkName"
                  :value="item.networkName"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="userType == 1" label="所属客户" prop="nickName">
              <el-select v-model="form.nickName" placeholder="请选择客户">
                <el-option
                  v-for="item in khList"
                  :key="item.id"
                  :label="item.clientName"
                  :value="item.clientName"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="用户名称" prop="userName">
              <el-input v-model="form.userName" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="'0'">正常</el-radio>
                <el-radio :label="'1'">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="角色" prop="roleIds">
              <el-select v-model="form.roleIds" placeholder="请选择角色">
                <el-option
                  v-for="item in roleOptions"
                  :key="item.roleId"
                  :label="item.roleName"
                  :value="item.roleId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div class="btn">
            <el-button type="primary" round @click="save">保存</el-button>
            <el-button round @click="close">取消</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  listRole,
  getRole,
  delRole,
  addRole,
  updateRole,
} from "@/api/system/role";
import {
  treeselect as menuTreeselect,
  roleMenuTreeselect,
} from "@/api/system/menu";
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  resetUserPwd,
  changeUserStatus,
  deptTreeSelect,
} from "@/api/system/user";
import { getOutletsList } from "@/api/dashboard";
import { getkhlist } from "@/api/apifile/Networkfinance";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "User",
  data() {
    return {
      pageDate: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      tableData: [],
      loading: false,
      maskFlag: false,
      title: "",
      roleOptions: [],
      form: {
        nickName: "",
        userName: "",
        status: "",
        roleIds: null,
      },
      userType: 0,
      defaultProps: {
        children: "children",
        label: "label",
      },
      menuOptions: [],
      rules: {
        nickName: [{ required: true, message: "请选择承包区" }],
        userName: [{ required: true, message: "请输入用户昵称" }],
        status: [{ required: true, message: "请选择状态" }],
        roleIds: [{ required: true, message: "请选择角色" }],
      },
      networkList: [],
      khList: [],
    };
  },
  mounted() {
    this.getTableData();
    this.getOutletsListData();
    this.getkhlistData();
  },
  methods: {
    getkhlistData() {
      getkhlist().then((res) => {
        this.khList = res.data;
      });
    },
    getOutletsListData() {
      getOutletsList().then((res) => {
        this.networkList = res.data;
      });
    },
    getMenuTreeselect() {
      menuTreeselect().then((response) => {
        this.menuOptions = response.data;
      });
    },
    save() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.form.roleIds = [this.form.roleIds];
          if (this.form.userId != undefined) {
            updateUser(this.form).then((response) => {
              this.$message.success("修改成功");
              this.close();
              this.getTableData();
            });
          } else {
            addUser(this.form).then((response) => {
              this.$message.success("新增成功");
              this.close();
              this.getTableData();
            });
          }
        } else {
          return false;
        }
      });
    },
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    add() {
      this.title = "添加用户";
      listRole({ pageNum: 1, pageSize: 10 }).then((res) => {
        this.roleOptions = res.rows;
      });
      this.maskFlag = true;
    },
    close() {
      this.maskFlag = false;
      this.form = {};
    },
    getTableData() {
      this.loading = true;
      listUser(this.pageDate).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 根据角色ID查询菜单树结构 */
    getRoleMenuTreeselect(roleId) {
      return roleMenuTreeselect(roleId).then((response) => {
        this.menuOptions = response.menus;
        return response;
      });
    },
    edit(row) {
      this.close();
      listRole({ pageNum: 1, pageSize: 10 }).then((res) => {
        this.roleOptions = res.rows;
      });
      const userId = row.userId;
      getUser(userId).then((res) => {
        this.form = res.data;
        this.form.roleIds = res.roleIds[0];
        this.title = "修改用户";
        this.maskFlag = true;
      });
    },
    del(row) {
      const userId = row.userId;
      delUser(userId).then(() => {
        this.$message.success("删除成功");
        this.getTableData();
      });
    },
    handleSizeChange(size) {
      this.pageDate.pageSize = size;
      this.getTableData();
    },
    handleCurrentChange(current) {
      this.pageDate.pageNum = current;
      this.getTableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.user {
  padding: 20px;
}
.header {
  h2 {
    text-align: center;
    color: skyblue;
  }

  hr {
    margin: 16px 0;
    color: skyblue;
  }
}
.footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  background: rgba($color: #000000, $alpha: 0.2);
  .flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
  }
  .title {
    padding-left: 12px;
    border-left: 6px solid skyblue;
    font-size: 24px;
    font-weight: bold;
  }
  i {
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
  }
  .table {
    margin: 80px auto;
    padding: 30px;
    width: 30%;
    border-radius: 24px;
    background-color: #fff;
    .custom {
      margin-bottom: 20px;
      padding: 0 20px;
      .btn {
        display: flex;
        justify-content: space-around;
        .el-button {
          padding: 12px 36px;
          font-size: 16px;
        }
      }
    }
  }
  &::v-deep .t-table {
    .t-table__header {
      tr th {
        font-size: 16px;
        font-weight: bolder;
      }
    }
    .t-table__body {
      tr td {
        font-size: 16px;
        font-weight: bolder;
        color: #666;
      }
    }
  }
}
</style>
